package com.tz.dao;

import com.tz.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by sztzedu on 16-10-10.
 */
//@Component
@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired //将spring容器中已配好的 sessionFactory 注入进来
    private SessionFactory sessionFactory;

    public AccountDaoImpl() {
        System.out.println("AccountDaoImpl 空参构造...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private Session getSession() {
//        return MyThreadLocal.get();
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    @Transactional //当前方法配置为声明式事务
    public void save(Account account) {
        getSession().save(account);
    }

    @Override
    @Transactional //当前方法配置为声明式事务
    public void update(Account account) {
        getSession().update(account);
    }

    @Override
    @Transactional //当前方法配置为声明式事务
    public Account get(int id) {
        return (Account) getSession().get(Account.class,id);
    }


}
